package br.unifor.cct.mia.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException ;
import java.io.InputStreamReader;

public class EEGConverter {
    
    public static void main(String[] args) throws IOException {
        String setName = "Z";
        String setDir = "C:\\Pedro\\EEG Sets\\"+setName+"\\";
        
        File fileResult = new File(setDir+"result_"+setName+".txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileResult));
        
        writer.write("@RELATION EEG\n");
        for (int i=0; i<4097; i++) {
        	writer.write("@ATTRIBUTE ATT_"+i+" REAL\n");
        }
        
        writer.write("\n@DATA\n");
        for ( int i=1;i<=100;i++ ) {
            String n = String.valueOf(i);
            while ( n.length()<3 ) {
                n = "0"+n;
            }
            
            String setData = setDir+setName+n+".txt";
            BufferedReader reader = createReader(setData);
            
            String line = reader.readLine();
            int lineCount = 1;
            while ( line != null ) {
                writer.write(line);                
                line = reader.readLine();
                if ( line!=null ) {
                    writer.write(",");    
                    lineCount++;
                }
                else writer.write("\n");
            }
        }
        
        writer.flush();
        writer.close();
        System.out.println("Done.");
    }
    
    private static BufferedReader createReader(String setData) {
        BufferedReader reader = null;
        FileInputStream fIn;
        try {
            fIn = new FileInputStream(setData);
            reader = new BufferedReader(new InputStreamReader(fIn));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }    
        return reader;
    }

}
